using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Net;
using System.IO;

namespace HTTPSample
{
    class Program
    {
        static void Main(string[] args)
        {
            string requestLocation = "http://dev-w2003-32-vm/TimesheetHTTPInterface/BTSHTTPReceive.dll";

            // create an XML object
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.PreserveWhitespace = true;

            // create the root node named PO
            XmlNode tempXmlNode = xmlDocument.CreateNode(XmlNodeType.Element, "PO", "http://www.MoustafaRefaat/HTTPSample");
            xmlDocument.AppendChild(tempXmlNode);

            // create the Item element
            tempXmlNode = xmlDocument.CreateNode(XmlNodeType.Element, "Item", "");
            tempXmlNode.InnerText = "A";
            xmlDocument.DocumentElement.AppendChild(tempXmlNode);

            // create the Price element
            tempXmlNode = xmlDocument.CreateNode(XmlNodeType.Element, "Price", "");
            tempXmlNode.InnerText = "250";
            xmlDocument.DocumentElement.AppendChild(tempXmlNode);

            try
            {

                // create a web request and set the method of the request to POST
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestLocation);
                request.Method = "POST";

                // encode the data and store the byte representation in an array
                ASCIIEncoding encoding = new ASCIIEncoding();
                byte[] requestData = encoding.GetBytes(xmlDocument.OuterXml);

                // set the content type of the data being posted.
                request.ContentType = "application/x-www-form-urlencoded";

                // set the content length of the string being posted
                request.ContentLength = xmlDocument.OuterXml.Length;

                
                Stream requestStream = request.GetRequestStream();
                requestStream.Write(requestData, 0, requestData.Length);

                // flush out the data to the underlying stream and close it
                requestStream.Flush();
                requestStream.Close();

                // retrieve the response from the web server
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                string str = string.Format("Result: {0}-  {1}", response.StatusCode, response.StatusDescription);
                Debug.WriteLine(str);
                Console.WriteLine(str);
                
            }
            catch (WebException wex)
            {
                Debug.WriteLine("Unable to complete web request. Web Exception error: " + wex.Message); 
                Console.WriteLine("Unable to complete web request. Web Exception error: " + wex.Message);

            }   
        }
    }
}
